from __future__ import unicode_literals
import youtube_dl

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '128',
    }],
}


def get_info_yt_video(url: str):
    ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'})

    with ydl:
        result = ydl.extract_info(url, download=False)  # We just want to extract the info

    if 'entries' in result:
        # Can be a playlist or a list of videos
        info = result['entries'][0]
    else:
        # Just a video
        info = result

    return {
        "is_live": info["is_live"],
        "link": url,
        "id": info["id"],
        "title": info["title"]
    }


def get_mp3_from_ut(video: dict):
    url = video["link"]
    with youtube_dl.YoutubeDL(ydl_opts) as ydl_mp3:
        ydl_mp3.download([url])

    return video


# print(get_info_yt_video("https://yewtu.be/watch?v=t6xt6Qksklk"))
